package cc.linker.steplink.crm.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常
 *
 * @author zhaomengwei
 */
@Getter
@Setter
@AllArgsConstructor
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "404")
public class GlobalException extends RuntimeException {
    private IResponseCode responseCode;
    private Object data;

    public GlobalException(IResponseCode responseCode) {
        this.responseCode = responseCode;
    }

    public static GlobalException build(int code, String msg) {
        return new GlobalException(new ResponseException(code, msg));
    }

    public static GlobalException build(String msg) {
        return build(400, msg);
    }

    public static GlobalException fail() {
        return new GlobalException(ResponseCode.FAIL);
    }

    public static GlobalException notFound() {
        return new GlobalException(ResponseCode.NOT_FOUND);
    }

    public static GlobalException notFound(String belongId) {
        Map<String, Object> map = new HashMap<>();
        map.put("belongId", belongId);
        return new GlobalException(ResponseCode.NOT_FOUND, map);
    }

    /*
    * 活动太火爆了
    * */
    public static GlobalException goodsFiery() {
        return new GlobalException(ResponseCode.GOODS_FIERY);
    }
}
